#include "accountmgr.h"
#include "cache.h"
#include "DbPart.h"
#include "minutelisten.h"



AccountMgr::AccountMgr()
{
    MinuteListen::Instance()->Register(__FUNCTION__, [&](std::int64_t tick){
        m_uid_mgr.OnTick();
    });
}
AccountMgr::~AccountMgr()
{
}

bool AccountMgr::Reg(const std::string& uuid, const std::uint32_t uid, const std::string& agency, const std::string& username, const std::uint32_t agency_id, const std::string& token, const std::string& hall_url, const std::string& platform, const std::int64_t balance, const std::string& currency, const std::string& ext)
{
    // 同步数据库
    if (!CDbPart::Instance()->RegisterNewPlayer(uuid, uid, agency, username, agency_id))
    {
        return false;
    }
    // 同步缓存
    return Cache::Instance()->Set(uuid, uid, agency, username, agency_id, token, hall_url, platform, balance, currency, ext);
}
bool AccountMgr::Update(const std::string& uuid, const std::uint32_t uid, const std::string& agency, const std::string& username, const std::uint32_t agency_id, const std::string& token, const std::string& hall_url, const std::string& platform, const std::int64_t balance, const std::string& currency, const std::string& ext)
{
    // 同步缓存
    Cache::Instance()->InfoSet(uuid, uid, agency, username, agency_id, token, hall_url, platform, balance, currency, ext);

    return true;
}
bool AccountMgr::Get(const std::uint32_t uid, std::string& uuid, std::string& agency, std::string& username, std::uint32_t& agency_id, std::string& token, std::string& hall_url, std::string& platform, std::int64_t& balance, std::string& currency, std::string& ext)
{
    if (Cache::Instance()->InfoGet(uid, uuid, agency, username, agency_id, token, hall_url, platform, balance, currency, ext))
    {
        return true;
    }
    return false;
}
bool AccountMgr::Uid(const std::string& uuid, std::uint32_t& uid)
{
    // 缓存
    if (!Cache::Instance()->Uid(uuid, uid))
        return false;

    if (uid > 0) 
        return true;
    //db
    if (!CDbPart::Instance()->GetUserId(uuid, uid))
        return false;

    if (uid > 0)
    {
        Cache::Instance()->UidSet(uuid, uid);
    }
    return true;
}
std::string AccountMgr::Uuid(const std::string& agency, const std::string& username)
{
    return agency + ":" + username;
}
bool AccountMgr::GetUserByUid(const std::uint32_t uid, std::string& uuid, std::string& agency, std::string& username, std::uint32_t& agency_id)
{
    stAccount* p_acc = m_uid_mgr.Get(uid, 0);
    if (p_acc != nullptr)
    {
        uuid = p_acc->uuid;
        agency = p_acc->agency;
        username = p_acc->username;
        agency_id = p_acc->agency_id;
        return true;
    }
    if (!CDbPart::Instance()->GetUserByUid(uid, uuid, agency, username, agency_id))
    {
        return false;
    }
    if (!uuid.empty())
    {
        stAccount acc;
        acc.uid = uid;
        acc.uuid = uuid;
        acc.agency = agency;
        acc.username = username;
        acc.agency_id = agency_id;
    }
    return true;
}

bool AccountMgr::GetUserByUuid(const std::string& uuid, std::uint32_t& uid, std::string& agency, std::string& username, std::uint32_t& agency_id)
{
    stAccount* p_acc = m_uuid_mgr.Get(uuid, 0);
    if (p_acc != nullptr)
    {
        uid = p_acc->uid;
        agency = p_acc->agency;
        username = p_acc->username;
        agency_id = p_acc->agency_id;
        return true;
    }
    if (!CDbPart::Instance()->GetUserByUuid(uuid, uid, agency, username, agency_id))
    {
        return false;
    }
    if (uid != 0)
    {
        stAccount acc;
        acc.uid = uid;
        acc.uuid = uuid;
        acc.agency = agency;
        acc.username = username;
        acc.agency_id = agency_id;
    }
    return true; 
}









